{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 大数据基础 - 上机实验\n",
    "\n",
    "### Course ID: UG_BGD_2024"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下方填写姓名和学号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Student_name = \"李卓\"\n",
    "Student_id = \"2335040120\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在`YOUR CODE HERE`及 \"YOUR ANSWER HERE\" 标注的位置补全代码。  \n",
    "作业提交后由程序自动评分，提交前应执行“Validate”进行代码自检"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "作业提交操作教程（首次作业需观看）：[https://www.bilibili.com/video/BV1dH4y1X7QG](https://www.bilibili.com/video/BV1dH4y1X7QG)\n",
    "\n",
    "作业提交网站：http://quiz.zhangys.org.cn\n",
    "\n",
    "登陆用户名：学号  \n",
    "**初始登陆密码为空，首次登陆后必须更改密码（不能与学号相同），否则无法下载题目和提交作业**  \n",
    "**不要修改文件名，提交的作业文件必须与题目文件同名，否则将影响机判**  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "鼓励学习交流，严禁照搬抄袭。  \n",
    "Peer review is encouraged; plagiarism is PROHIBITED.  \n",
    "如发现雷同作业，则抄袭者和被抄袭者当次作业均判为0分。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本次练习着重考察Python的常用语法及函数编写方法\n",
    "\n",
    "\n",
    "参考资源：  \n",
    "Python的一个在线教程： https://www.w3school.com.cn/python/index.asp  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem (2 points)\n",
    "\n",
    "如果一个3位数等于其各位数字的立方和，则称这个数为水仙花数。水仙花数（Narcissistic number）也被称为超完全数字不变数（pluperfect digital invariant, PPDI）、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数（Armstrong number）. 例如:153 = 1 + 125 + 27，因此153就是一个水仙花数。\n",
    "\n",
    "完成函数ppdi(), 判断一个三位数是否为水仙数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "9f20f6265cd84a8ed06f830296db3087",
     "grade": false,
     "grade_id": "cell-336a1ec568d4c2ea",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def ppdi(num):\n",
    "    return num==((num//100)**3+(num//10%10)**3+(num%10)**3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "fae58cb7ff9e512b13505739dd965a50",
     "grade": true,
     "grade_id": "cell-ef7dba9eb399d58e",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"测试用例\"\"\"\n",
    "assert ppdi(153) == True\n",
    "assert ppdi(999) == False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "markdown",
     "checksum": "e4ab61f29bb3424f2c3a44f64c8be605",
     "grade": false,
     "grade_id": "cell-56ce3e6af818d022",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Problem (2 points)\n",
    "\n",
    "完成函数sum_squares(n),返回前n个自然数的平方和（又称为pyramid number）。即：sum_squares(n) = $\\sum_{i=1}^n i^2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "61518483377c71d598fe180af7934cb5",
     "grade": false,
     "grade_id": "cell-ddbe9e9f9c838313",
     "locked": false,
     "schema_version": 3,
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "def sum_squares(n):\n",
    "    sum=0\n",
    "    for i in range(n+1):\n",
    "        sum+=i**2\n",
    "    return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "203034a06506d5e61c1fd93e96859b90",
     "grade": true,
     "grade_id": "cell-effec927dd41d301",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"测试用例\"\"\"\n",
    "assert sum_squares(1) == 1\n",
    "assert sum_squares(3) == 1+2*2+3*3\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "markdown",
     "checksum": "2c7752d18212a17da6e7f85592e61c39",
     "grade": false,
     "grade_id": "cell-36914fe6807e7e0f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Problem (2 points)\n",
    "\n",
    "设year为任意一年的公元年号，若year满足下面两个条件中的任意一个，则该年是闰年。若两个条件都不满足，则该年不是闰年。\n",
    "闰年的条件是：  \n",
    "  ①能被4整除，但不能被100整除。  \n",
    "  ②能被400整除\n",
    "  \n",
    "完成函数leap(n)判断用户输入的年份是否是闰年. n是闰年，返回True；否则返回False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "dfacb51d622c08dc1c0385028ff9c456",
     "grade": false,
     "grade_id": "leap",
     "locked": false,
     "schema_version": 3,
     "solution": true
    },
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "def leap(year):\n",
    "    return (year%4==0 and year%100!=0) or (year%400==0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "688856474c87fe2ecaf44ce28b962566",
     "grade": true,
     "grade_id": "leap_test1",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": false
    },
    "slideshow": {
     "slide_type": "notes"
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"测试用例\"\"\"\n",
    "assert leap(1999) == False\n",
    "assert leap(1024) == True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "7228253ca1b2986fe8b7bb10129c6093",
     "grade": true,
     "grade_id": "leap_test2",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"测试用例\"\"\"\n",
    "assert leap(200) == False\n",
    "assert leap(800) == True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem (2 point)\n",
    "\n",
    "实现函数find_primes(n), 找出n以内的质数数量。如 find_primes(11) 返回5（11以内包括2，3，5，7，11四个质数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "bb9eefa260d54246ab96d347044bdbbf",
     "grade": false,
     "grade_id": "cell-6670709c0c90466e",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def find_primes(n):\n",
    "    count=0\n",
    "    for i in range(2,n+1):\n",
    "        flag=True\n",
    "        for j in range(2,i):\n",
    "            if i%j==0:\n",
    "                flag=False\n",
    "                break\n",
    "        if flag:\n",
    "            count+=1\n",
    "    return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "566948ce4fd1e8fa8f21d6a996bf49f7",
     "grade": true,
     "grade_id": "cell-f4fb94557a2320d0",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert find_primes(10) == 4 # 2、3、5、7\n",
    "assert find_primes(23) == 9 # 2、3、5、7、11、13、17、19、23\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem (2 point)\n",
    "\n",
    "统计字符串中特定字母出现的次数。区分大小写。如 count_letter(\"Hello, welcome to the west world.\", 'w') = 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "1ef3a6f8a134d84911b8c96041c8c54e",
     "grade": false,
     "grade_id": "cell-9cff203cd726c51f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def count_letter(s, ch):\n",
    "    count=0\n",
    "    for i in s :\n",
    "        if i == ch :\n",
    "            count+=1\n",
    "    return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "10a5867af1f4dce8ef31a0085a6763e0",
     "grade": true,
     "grade_id": "cell-a10d1dd7281a7e41",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert count_letter('Hello, welcome to my world.', 'w') == 2\n",
    "assert count_letter('Hello Kitty', 'K') == 1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem (2 point)\n",
    "\n",
    "2023杭州亚运期间实施了单双号限行政策，即单号日通行尾号单数车辆，双号日通行尾号偶数车辆。  \n",
    "实现函数check_plate(s)判断车辆是否为尾号单数，返回值为True/False。如 check_plate(\"浙A9999B\") = True, check_plate(\"浙A999B2\") = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "8cbeee2bd6941acb400429f16d831ac4",
     "grade": false,
     "grade_id": "cell-37a0f72a4bfd033a",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def check_plate(s):\n",
    "    if 'A'<s[-1]<'Z':\n",
    "        a=ord(s[-1])-ord(\"A\")\n",
    "        return a%2==1\n",
    "    return int(s[-1])%2==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "58eafe721d7781298af14b104f47e4f0",
     "grade": true,
     "grade_id": "cell-5b7463dab2028a88",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert check_plate(\"浙A9999B\") == True\n",
    "assert check_plate(\"沪A999B2\") == False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem (2 point)\n",
    "\n",
    "实现函数palindrome(s)判断字符串s是否为回文，要求不区分大小写。回文是正读反读相同的字符串，如'aba','Aba','blahalb'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "1c51fa06623e5f93c0b55c24c07147b6",
     "grade": false,
     "grade_id": "palindrome",
     "locked": false,
     "schema_version": 3,
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "def palindrome(s):\n",
    "    a=s.lower()\n",
    "    return a==a[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "29c780ce70b86cdca1a9d73cff98e999",
     "grade": true,
     "grade_id": "palindrome_test",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "assert palindrome('abb') == False\n",
    "assert palindrome('Hannah') == True\n",
    "assert palindrome('床前明月光') == False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Problem (3 points)\n",
    "\n",
    "完成函数高斯函数gaussian(x, mu, s)，计算概率p。 传入参数mu为均值、s为标准差\n",
    "\n",
    "$$\n",
    "P(x) = {1 \\over \\sqrt{2 \\pi \\sigma^2}} e ^ { - {{ (x - \\mu)^2 } \\over {2 \\sigma^2}}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "4f0472f6dcc0b31cd0b26473547e820f",
     "grade": false,
     "grade_id": "cell-b3b7129f7f1652de",
     "locked": false,
     "schema_version": 3,
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "def gaussian(x, mu, s):\n",
    "    \n",
    "   # 确保标准差是正数\n",
    "    s = np.abs(s)\n",
    "    # 计算概率密度\n",
    "    probability = (1 / (s * np.sqrt(2 * np.pi))) * np.exp(-((x - mu)**2) / (2 * s**2))\n",
    "    return probability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "816dae178c8fda6757a14ce25cc4c5c1",
     "grade": true,
     "grade_id": "cell-07ba3ab98f8414ad",
     "locked": true,
     "points": 3,
     "schema_version": 3,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "assert round(gaussian(0, 0, 1), 3) == 0.399\n",
    "assert round(gaussian(1, 0, 1), 3) == 0.242\n",
    "assert round(gaussian(-3, 0, 1), 3) == 0.004\n",
    "assert round(gaussian(3, 0, 1), 3) == 0.004\n",
    "assert round(gaussian(-3, -2, 1), 3) == 0.242\n",
    "assert round(gaussian(0, 0, 2), 3) == 0.199\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem (3 points) Numpy库的使用\n",
    "\n",
    "矩阵与向量的乘法运算，对应向量的几何变换\n",
    "\n",
    "$ A = \\begin{bmatrix} a & b & c \\\\ d & e & f \\end{bmatrix} $， $ v = \\begin{bmatrix} 1 \\\\ x \\\\ y \\end{bmatrix} $\n",
    "\n",
    "define $ v' = Av = \\begin{bmatrix} x' \\\\ y' \\end{bmatrix} $, then $ v' = Av = \\begin{bmatrix} a & b & c \\\\ d & e & f \\end{bmatrix}  \\begin{bmatrix} 1 \\\\ x \\\\ y \\end{bmatrix}  = \\begin{bmatrix} a + bx + cy \\\\ d + ex + fy \\end{bmatrix} $\n",
    "\n",
    "i.e., $ x' = a + bx + cy $, $ y' = d + ex + fy $\n",
    "\n",
    "so, $ v' = Av = \\begin{bmatrix} x' \\\\ y' \\end{bmatrix} \n",
    "= \\begin{bmatrix} a \\\\ d \\end{bmatrix}  + \\begin{bmatrix} b & c \\\\ e & f \\end{bmatrix}  \\begin{bmatrix} x \\\\ y \\end{bmatrix} \n",
    " $\n",
    "\n",
    "\n",
    "$ \\begin{bmatrix} a \\\\ d \\end{bmatrix} $ corresponds to translation（平移）/ bias（偏置）,   Matrix $\\begin{bmatrix} b & c \\\\ e & f \\end{bmatrix} $ corresponda to a Linear Transformation (can be decomposed to scaling（缩放） $\\begin{bmatrix} s1 & 0 \\\\ 0 & s2 \\end{bmatrix} $, rotation（旋转） $\\begin{bmatrix} cos(\\theta) & -sin(\\theta) \\\\ sin(\\theta) & cos(\\theta) \\end{bmatrix} $, shear（错切） $\\begin{bmatrix} 1 & s \\\\ 0 & 1 \\end{bmatrix} $, etc.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "markdown",
     "checksum": "7e0a72dfe6ec3fdfdacb393b985a775b",
     "grade": false,
     "grade_id": "cell-6caa5da67bf3ea4d",
     "locked": true,
     "schema_version": 3,
     "solution": false
    }
   },
   "source": [
    "练习：使用Numpy库完成函数`scale_rotate_vector(v, scale, angle)`, 该函数将二维列向量`v`等比例缩放`scale`倍，并旋转`angle`度。  \n",
    "\n",
    "示例：\n",
    "\n",
    "$$\n",
    "v = \\begin{bmatrix} 1 \\\\ 2 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "当scale = 2, angle = 90时, `scale_rotate_vector(v, 2, 90)`的返回值：\n",
    "   \n",
    "\n",
    "$$\n",
    "v' = \\begin{bmatrix} -4 \\\\ 2 \\end{bmatrix}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "43d002feeae53e6b4facd68c6a0e69fc",
     "grade": false,
     "grade_id": "cell-3f9dd27966874fd0",
     "locked": false,
     "schema_version": 3,
     "solution": true
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "\n",
    "def scale_rotate_vector(v, scale, angle):\n",
    "        # 转换角度为弧度\n",
    "    angle_rad = np.deg2rad(angle)\n",
    "    \n",
    "    # 计算旋转矩阵\n",
    "    cos_angle = np.cos(angle_rad)\n",
    "    sin_angle = np.sin(angle_rad)\n",
    "    rotation_matrix = np.array([[cos_angle, -sin_angle],\n",
    "                                [sin_angle,  cos_angle]])\n",
    "    \n",
    "    # 缩放向量\n",
    "    scaled_v = v * scale\n",
    "    \n",
    "    # 旋转向量\n",
    "    rotated_v = np.dot(rotation_matrix, scaled_v)\n",
    "    \n",
    "    return rotated_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "f23e0191e6abf0499570a094da5e4716",
     "grade": true,
     "grade_id": "cell-fd0415c8f6efcdbc",
     "locked": true,
     "points": 3,
     "schema_version": 3,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "v = np.array([[1],[2]])\n",
    "\n",
    "vt = scale_rotate_vector(v, -2, 0)\n",
    "assert np.allclose(vt, np.array([[-2],[-4]]))\n",
    "\n",
    "vt = scale_rotate_vector(v, 2, 90)\n",
    "assert np.allclose(vt, np.array([[-4],[2]]))\n",
    "\n",
    "v = np.array([[1],[1]])\n",
    "\n",
    "vt = scale_rotate_vector(v, 1/math.sqrt(2), 45)\n",
    "assert np.allclose(vt, np.array([[0],[1]]))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "markdown",
     "checksum": "88ec0d4efdd132c84aa3e46b817768fa",
     "grade": false,
     "grade_id": "cell-abac125550bfdf64",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Problem (2 points) 阶乘和的计算 \n",
    "完成函数，返回前n个数的阶乘和。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "6b47fc397eb7f81e6f1b8851915d8c49",
     "grade": false,
     "grade_id": "cell-29d1b07d8b632a33",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def factorial_sum (n):\n",
    "    sum=0\n",
    "    for i in range(1,n+1):\n",
    "        jie = 1\n",
    "        for j in range(1,i+1):\n",
    "            jie=jie*j\n",
    "        sum+=jie\n",
    "    return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "350dfaa97d85eafdad1dd092edac6199",
     "grade": true,
     "grade_id": "cell-59217f0a7152bf9d",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert factorial_sum(3)==9\n",
    "assert factorial_sum(4)==33\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# problem(2 points) 求最大公约数\n",
    "设计函数，求出任意两个数之间的最大公约数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "a48c588338d05229a36cf09a51076865",
     "grade": false,
     "grade_id": "cell-4c898d531dbd0101",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def f(n1,n2):  \n",
    "    if n1>n2:\n",
    "        n1,n2=n2,n1\n",
    "    temp = n1%n2\n",
    "    while temp!=0:\n",
    "        n1=n2\n",
    "        n2=temp\n",
    "        temp=n1%n2\n",
    "    return n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "66b687a892dfad14c00f29609d23a537",
     "grade": true,
     "grade_id": "cell-383e6a77bf05444e",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert f(23,46)==23\n",
    "assert f(26,97)==1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# problem(2 points)\n",
    "设计函数，计算下式首项为a，公比为q的等比数列前n项和。(q≠1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "7bc30424df41b6236dc65851f4aa051f",
     "grade": false,
     "grade_id": "cell-96ae0a88741a17fa",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def nsum(a,q,n):\n",
    "    return a*(1-q**n)/(1-q)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "4dad6eb03a537f6ee4d497a17454151d",
     "grade": true,
     "grade_id": "cell-0ec1f0307bb5883b",
     "locked": true,
     "points": 2,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert nsum(1,2,3)==7\n",
    "assert nsum(5,7,3)==285"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Problem (3 points)\n",
    "\n",
    "定义函数 get_digit_frequency()\n",
    "\n",
    "该函数统计一组数字中，1 ~ 9首位出现的次数。 输入为数组（包括整数和浮点数，不必考虑负数）。输出为长度为9的list，对应1 ~ 9首位出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "deletable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "2555fa9b9bede1d1afde5e7fdb13b8e2",
     "grade": false,
     "grade_id": "cell-5174d652908a30a5",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def get_digit_frequency(arr):\n",
    "\n",
    "    cnts = [0]*9\n",
    "\n",
    "    for i in arr:\n",
    "        if i>1:\n",
    "            n=int(str(i)[0])\n",
    "            cnts[n-1]+=1\n",
    "        else:\n",
    "            n=i\n",
    "            while n<1:\n",
    "                n*=10\n",
    "            cnts[int(n-1)]+=1\n",
    "\n",
    "    return cnts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "deletable": false,
    "editable": false,
    "nbgrader": {
     "cell_type": "code",
     "checksum": "880b3f119d29dfc6a200963d8c757fb1",
     "grade": true,
     "grade_id": "cell-bed7d87134d28c36",
     "locked": true,
     "points": 3,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert get_digit_frequency([1098,33053]) == [1, 0, 1, 0, 0, 0, 0, 0, 0]\n",
    "assert get_digit_frequency([1,1098,0.2]) == [2, 1, 0, 0, 0, 0, 0, 0, 0]\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
